# -*- coding: utf-8 -*-

"""
@author: whq
@contact: whq1360071755@outlook.com
@version: 1.0.0
@license: Apache Licence
@file: participle.py
@time: 2023/5/11 17:03
@describe: CRF Participle model
"""

import sklearn_crfsuite
from sklearn_crfsuite import scorers
from sklearn_crfsuite import metrics
from dataset.corpus_2014 import Corpus2014Dataset


def word2features(sent, i):
    word = sent[i][0]

    features = {
        'bias': 1.0,
        'word.lower()': word.lower(),
        'word[-3:]': word[-3:],
        'word[-2:]': word[-2:],
        'word.isupper()': word.isupper(),
        'word.istitle()': word.istitle(),
        'word.isdigit()': word.isdigit(),
    }
    if i > 0:
        word1 = sent[i-1][0]
        features.update({
            '-1:word.lower()': word1.lower(),
            '-1:word.istitle()': word1.istitle(),
            '-1:word.isupper()': word1.isupper(),
        })
    else:
        features['BOS'] = True

    if i < len(sent)-1:
        word1 = sent[i+1][0]
        features.update({
            '+1:word.lower()': word1.lower(),
            '+1:word.istitle()': word1.istitle(),
            '+1:word.isupper()': word1.isupper(),
        })
    else:
        features['EOS'] = True

    return features


def sent2features(sent):
    return [word2features(sent, i) for i in range(len(sent))]


def sent2labels(sent):
    return [label for token, label in sent]


def sent2tokens(sent):
    return [token for token, label in sent]


if __name__ == '__main__':
    ds = Corpus2014Dataset()
    X_train = [sent2features(s) for train_sent in ds.train_data for s in train_sent]
    y_train = [sent2labels(s) for train_sent in ds.train_data for s in train_sent]

    X_test = [sent2features(s) for test_sent in ds.test_data for s in test_sent]
    y_test = [sent2labels(s) for test_sent in ds.test_data for s in test_sent]
